home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gtk-2.0 / gobject / propertyhelper.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  9.2 KB  |  294 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import gobject._gobject as gobject
  6. _gobject = sys.modules['gobject._gobject']
  7. from gobject.constants import TYPE_NONE, TYPE_INTERFACE, TYPE_CHAR, TYPE_UCHAR, TYPE_BOOLEAN, TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG, TYPE_INT64, TYPE_UINT64, TYPE_ENUM, TYPE_FLAGS, TYPE_FLOAT, TYPE_DOUBLE, TYPE_STRING, TYPE_POINTER, TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, TYPE_PYOBJECT
  8. from gobject.constants import G_MINFLOAT, G_MAXFLOAT, G_MINDOUBLE, G_MAXDOUBLE, G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, G_MAXULONG
  9.  
  10. class property(object):
  11.     """
  12.     Creates a new property which in conjunction with GObject subclass will
  13.     create a property proxy:
  14.  
  15.     >>> class MyObject(gobject.GObject):
  16.     >>> ... prop = gobject.property(type=str)
  17.  
  18.     >>> obj = MyObject()
  19.     >>> obj.prop = 'value'
  20.  
  21.     >>> obj.prop
  22.     'value'
  23.  
  24.     The API is similar to the builtin property:
  25.  
  26.     class AnotherObject(gobject.GObject):
  27.         @gobject.property
  28.         def prop(self):
  29.             return ...
  30.  
  31.     Which will create a read-only property called prop.
  32.     """
  33.     
  34.     class __metaclass__(type):
  35.         
  36.         def __repr__(self):
  37.             return "<class 'gobject.property'>"
  38.  
  39.  
  40.     
  41.     def __init__(self, getter = None, setter = None, type = None, default = None, nick = '', blurb = '', flags = _gobject.PARAM_READWRITE, minimum = None, maximum = None):
  42.         '''
  43.         @param  getter: getter to get the value of the property
  44.         @type   getter: callable
  45.         @param  setter: setter to set the value of the property
  46.         @type   setter: callable
  47.         @param    type: type of property
  48.         @type     type: type
  49.         @param default: default value
  50.         @param    nick: short description
  51.         @type     bick: string
  52.         @param   blurb: long description
  53.         @type    blurb: string
  54.         @param flags:    parameter flags, one of:
  55.         - gobject.PARAM_READABLE
  56.         - gobject.PARAM_READWRITE
  57.         - gobject.PARAM_WRITABLE
  58.         - gobject.PARAM_CONSTRUCT
  59.         - gobject.PARAM_CONSTRUCT_ONLY
  60.         - gobject.PARAM_LAX_VALIDATION
  61.         @keyword minimum:  minimum allowed value (int, float, long only)
  62.         @keyword maximum:  maximum allowed value (int, float, long only)
  63.         '''
  64.         if getter and not setter:
  65.             setter = self._readonly_setter
  66.         elif setter and not getter:
  67.             getter = self._writeonly_getter
  68.         elif not setter and not getter:
  69.             getter = self._default_getter
  70.             setter = self._default_setter
  71.         
  72.         self.getter = getter
  73.         self.setter = setter
  74.         if type is None:
  75.             type = object
  76.         
  77.         self.type = self._type_from_python(type)
  78.         self.default = self._get_default(default)
  79.         self._check_default()
  80.         if not isinstance(nick, basestring):
  81.             raise TypeError('nick must be a string')
  82.         isinstance(nick, basestring)
  83.         self.nick = nick
  84.         if not isinstance(blurb, basestring):
  85.             raise TypeError('blurb must be a string')
  86.         isinstance(blurb, basestring)
  87.         self.blurb = blurb
  88.         if flags < 0 or flags > 32:
  89.             raise TypeError('invalid flag value: %r' % (flags,))
  90.         flags > 32
  91.         self.flags = flags
  92.         if minimum is not None:
  93.             if minimum < self._get_minimum():
  94.                 raise TypeError('Minimum for type %s cannot be lower than %d' % (self.type, self._get_minimum()))
  95.             minimum < self._get_minimum()
  96.         else:
  97.             minimum = self._get_minimum()
  98.         self.minimum = minimum
  99.         if maximum is not None:
  100.             if maximum > self._get_maximum():
  101.                 raise TypeError('Maximum for type %s cannot be higher than %d' % (self.type, self._get_maximum()))
  102.             maximum > self._get_maximum()
  103.         else:
  104.             maximum = self._get_maximum()
  105.         self.maximum = maximum
  106.         self.name = None
  107.         self._values = { }
  108.         self._exc = None
  109.  
  110.     
  111.     def __repr__(self):
  112.         if not self.name:
  113.             pass
  114.         return '<gobject property %s (%s)>' % ('(uninitialized)', _gobject.type_name(self.type))
  115.  
  116.     
  117.     def __get__(self, instance, klass):
  118.         if instance is None:
  119.             return self
  120.         self._exc = None
  121.         value = instance.get_property(self.name)
  122.         if self._exc:
  123.             exc = self._exc
  124.             self._exc = None
  125.             raise exc
  126.         self._exc
  127.         return value
  128.  
  129.     
  130.     def __set__(self, instance, value):
  131.         if instance is None:
  132.             raise TypeError
  133.         instance is None
  134.         self._exc = None
  135.         instance.set_property(self.name, value)
  136.         if self._exc:
  137.             exc = self._exc
  138.             self._exc = None
  139.             raise exc
  140.         self._exc
  141.  
  142.     
  143.     def _type_from_python(self, type):
  144.         if type == int:
  145.             return TYPE_INT
  146.         if type == bool:
  147.             return TYPE_BOOLEAN
  148.         if type == long:
  149.             return TYPE_LONG
  150.         if type == float:
  151.             return TYPE_DOUBLE
  152.         if type == str:
  153.             return TYPE_STRING
  154.         if type == object:
  155.             return TYPE_PYOBJECT
  156.         if type == _gobject.GObject:
  157.             return TYPE_OBJECT
  158.         if type in [
  159.             TYPE_NONE,
  160.             TYPE_INTERFACE,
  161.             TYPE_CHAR,
  162.             TYPE_UCHAR,
  163.             TYPE_INT,
  164.             TYPE_UINT,
  165.             TYPE_BOOLEAN,
  166.             TYPE_LONG,
  167.             TYPE_ULONG,
  168.             TYPE_INT64,
  169.             TYPE_UINT64,
  170.             TYPE_ENUM,
  171.             TYPE_FLAGS,
  172.             TYPE_FLOAT,
  173.             TYPE_DOUBLE,
  174.             TYPE_POINTER,
  175.             TYPE_BOXED,
  176.             TYPE_PARAM,
  177.             TYPE_OBJECT,
  178.             TYPE_STRING,
  179.             TYPE_PYOBJECT]:
  180.             return type
  181.         raise TypeError('Unsupported type: %r' % (type,))
  182.  
  183.     
  184.     def _get_default(self, default):
  185.         ptype = self.type
  186.         if default is not None:
  187.             return default
  188.         if ptype in [
  189.             TYPE_INT,
  190.             TYPE_UINT,
  191.             TYPE_LONG,
  192.             TYPE_ULONG,
  193.             TYPE_INT64,
  194.             TYPE_UINT64]:
  195.             return 0
  196.         if ptype == TYPE_STRING:
  197.             return ''
  198.         if ptype == TYPE_FLOAT or ptype == TYPE_DOUBLE:
  199.             return 0
  200.         return None
  201.  
  202.     
  203.     def _check_default(self):
  204.         ptype = self.type
  205.         default = self.default
  206.         if ptype == TYPE_BOOLEAN and default not in (True, False):
  207.             raise TypeError('default must be True or False, not %r' % (default,))
  208.         default not in (True, False)
  209.         if ptype == TYPE_PYOBJECT:
  210.             if default is not None:
  211.                 raise TypeError('object types does not have default values')
  212.             default is not None
  213.         
  214.  
  215.     
  216.     def _get_minimum(self):
  217.         ptype = self.type
  218.         if ptype in [
  219.             TYPE_UINT,
  220.             TYPE_ULONG,
  221.             TYPE_UINT64]:
  222.             return 0
  223.         if ptype == TYPE_FLOAT:
  224.             return G_MINFLOAT
  225.         if ptype == TYPE_DOUBLE:
  226.             return G_MINDOUBLE
  227.         if ptype == TYPE_INT:
  228.             return G_MININT
  229.         if ptype == TYPE_LONG:
  230.             return G_MINLONG
  231.         if ptype == TYPE_INT64:
  232.             return -0x4000000000000001L
  233.  
  234.     
  235.     def _get_maximum(self):
  236.         ptype = self.type
  237.         if ptype == TYPE_UINT:
  238.             return G_MAXUINT
  239.         if ptype == TYPE_ULONG:
  240.             return G_MAXULONG
  241.         if ptype == TYPE_INT64:
  242.             return 0x3FFFFFFFFFFFFFFFL
  243.         if ptype == TYPE_UINT64:
  244.             return 0x7FFFFFFFFFFFFFFFL
  245.         if ptype == TYPE_FLOAT:
  246.             return G_MAXFLOAT
  247.         if ptype == TYPE_DOUBLE:
  248.             return G_MAXDOUBLE
  249.         if ptype == TYPE_INT:
  250.             return G_MAXINT
  251.         if ptype == TYPE_LONG:
  252.             return G_MAXLONG
  253.  
  254.     
  255.     def _default_setter(self, instance, value):
  256.         self._values[instance] = value
  257.  
  258.     
  259.     def _default_getter(self, instance):
  260.         return self._values.get(instance, self.default)
  261.  
  262.     
  263.     def _readonly_setter(self, instance, value):
  264.         self._exc = TypeError('%s property of %s is read-only' % (self.name, type(instance).__name__))
  265.  
  266.     
  267.     def _writeonly_getter(self, instance):
  268.         self._exc = TypeError('%s property of %s is write-only' % (self.name, type(instance).__name__))
  269.  
  270.     
  271.     def get_pspec_args(self):
  272.         ptype = self.type
  273.         if ptype in [
  274.             TYPE_INT,
  275.             TYPE_UINT,
  276.             TYPE_LONG,
  277.             TYPE_ULONG,
  278.             TYPE_INT64,
  279.             TYPE_UINT64,
  280.             TYPE_FLOAT,
  281.             TYPE_DOUBLE]:
  282.             args = (self._get_minimum(), self._get_maximum(), self.default)
  283.         elif ptype == TYPE_STRING or ptype == TYPE_BOOLEAN:
  284.             args = (self.default,)
  285.         elif ptype == TYPE_PYOBJECT:
  286.             args = ()
  287.         elif ptype == TYPE_OBJECT:
  288.             args = ()
  289.         else:
  290.             raise NotImplementedError(ptype)
  291.         return ((ptype == TYPE_BOOLEAN).type, self.nick, self.blurb) + args + (self.flags,)
  292.  
  293.  
  294.